package com.ruoyi.hcmain.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.hcmain.domain.HcDispatch;
import com.ruoyi.hcmain.domain.HcStock;
import com.ruoyi.hcmain.domain.HcStuff;
import com.ruoyi.hcmain.service.IHcDispatchService;
import com.ruoyi.hcmain.service.IHcStockService;
import com.ruoyi.hcmain.service.IHcStuffService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hcmain.domain.HcReceive;
import com.ruoyi.hcmain.service.IHcReceiveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收录信息Controller
 * 
 * @author kylin
 * @date 2023-08-14
 */
@RestController
@RequestMapping("/hcmain/receive")
public class HcReceiveController extends BaseController
{
    @Autowired
    private IHcReceiveService hcReceiveService;
    @Autowired
    private IHcDispatchService hcDispatchService;
    @Autowired
    private IHcStockService iHcStockService;
    @Autowired
    private IHcStuffService iHcStuffService;

    /**
     * 查询收录信息列表
     */
    @PreAuthorize("@ss.hasPermi('hcmain:receive:list')")
    @GetMapping("/list")
    public TableDataInfo list(HcReceive hcReceive)
    {
        startPage();
        List<HcReceive> list = hcReceiveService.selectHcReceiveList(hcReceive);
        return getDataTable(list);
    }
    @GetMapping("/list/getsel")
    public AjaxResult listsel(HcReceive hcReceive)
    {
        List<HcStock> hcStocks= iHcStockService.selectHcStockList(null);
        List<Map<String,String>> maps=new ArrayList<>();
        for (HcStock hcStock:hcStocks){
            Map<String,String> map=new HashMap<>();
            map.put("value",hcStock.getStockId().toString());
            map.put("label",hcStock.getName());
            maps.add(map);
        }
        return success(maps);
    }
    @GetMapping("/list/getstu")
    public AjaxResult liststu(HcReceive hcReceive)
    {
        List<HcStuff> hcStuffList= iHcStuffService.selectHcStuffList(null);
        List<Map<String,String>> maps=new ArrayList<>();
        for (HcStuff hcStocks:hcStuffList){
            Map<String,String> map=new HashMap<>();
            map.put("value",hcStocks.getStuffId().toString());
            map.put("label",hcStocks.getSapnum());
            maps.add(map);
        }
        return success(maps);
    }
    @GetMapping("/list/getstuname")
    public AjaxResult liststuname(HcStuff hcStuff)
    {
        List<HcStuff> hcStuffList= iHcStuffService.selectHcStuffList(hcStuff);
        List<Map<String,String>> maps=new ArrayList<>();
        for (HcStuff hcStocks:hcStuffList){
            Map<String,String> map=new HashMap<>();
            map.put("value",hcStocks.getStuffId().toString());
            map.put("label",hcStocks.getName());
            maps.add(map);
        }
        return success(maps);
    }

    /**
     * 导出收录信息列表
     */
    @PreAuthorize("@ss.hasPermi('hcmain:receive:export')")
    @Log(title = "收录信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HcReceive hcReceive)
    {
        List<HcReceive> list = hcReceiveService.selectHcReceiveList(hcReceive);
        ExcelUtil<HcReceive> util = new ExcelUtil<HcReceive>(HcReceive.class);
        util.exportExcel(response, list, "收录信息数据");
    }

    /**
     * 获取收录信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('hcmain:receive:query')")
    @GetMapping(value = "/{receiveId}")
    public AjaxResult getInfo(@PathVariable("receiveId") Long receiveId)
    {
        return success(hcReceiveService.selectHcReceiveByReceiveId(receiveId));
    }
    @GetMapping(value = "/alldates")
    public AjaxResult getdates()
    {
        Calendar calendar = Calendar.getInstance();
// 获取当前年
        int year = calendar.get(Calendar.YEAR);
// 获取当前月
        int month = calendar.get(Calendar.MONTH) + 1;
        String noem="";
        if (month<10){
            noem = "0"+month;
        }else {
            noem = ""+month;
        }
        int days=getDays(year,month);
        String sday="";
        List<Map<String,String>> maps=new ArrayList<>();
        for (int j = 1; j <= days; j++) {
            Map<String,String> map=new HashMap<>();
            if (j<10){
                sday = "0"+j;
            }else {
                sday = ""+j;
            }
           String nowdate=year+"-"+noem+"-"+sday;
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            Date d;
            try {
                d =df.parse(nowdate);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            HcReceive hcReceive=new HcReceive();
            hcReceive.setCreateTime(d);
            HcDispatch hcDispatch=new HcDispatch();
            hcDispatch.setCreateTime(d);
            List<HcReceive> hcReceive1=hcReceiveService.selectHcReceiveByCreateTime(hcReceive);
            List<HcDispatch> list=hcDispatchService.selectHcDispatchByCreateTime(hcDispatch);
            String val="0";
            String valz="0";
            if(hcReceive1.size()>0){
                float mbig=0.00f;
                for (HcReceive h1s:hcReceive1){
                    if(null!=h1s.getPrices()){
                        float pr=h1s.getPrices().floatValue();
                        mbig+=pr;
                    }
                }
                val=String.format("%.2f", mbig);
            }
            if(list.size()>0){
                float mbigz=0.00f;
                for (HcDispatch h2c:list){
                    if(null!=h2c.getPrices()){
                        float pr1=h2c.getPrices().floatValue();
                        mbigz+=pr1;
                    }
                }
                valz=String.format("%.2f", mbigz);
            }
            map.put("dates",nowdate);
            map.put("re",val);
            map.put("di",valz);
            maps.add(map);
        }
        return success(maps);
    }
    public int getDays(int year, int month) {
        int days = 0;
        if (month != 2) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;

            }
        } else {
            // 闰年
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                days = 29;
            else
                days = 28;
        }
        System.out.println("当月有" + days + "天！");
        return days;
    }

    /**
     * 新增收录信息
     */
    @PreAuthorize("@ss.hasPermi('hcmain:receive:add')")
    @Log(title = "收录信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HcReceive hcReceive)
    {
        return toAjax(hcReceiveService.insertHcReceive(hcReceive));
    }

    /**
     * 修改收录信息
     */
    @PreAuthorize("@ss.hasPermi('hcmain:receive:edit')")
    @Log(title = "收录信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HcReceive hcReceive)
    {
        return toAjax(hcReceiveService.updateHcReceive(hcReceive));
    }

    /**
     * 删除收录信息
     */
    @PreAuthorize("@ss.hasPermi('hcmain:receive:remove')")
    @Log(title = "收录信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{receiveIds}")
    public AjaxResult remove(@PathVariable Long[] receiveIds)
    {
        return toAjax(hcReceiveService.deleteHcReceiveByReceiveIds(receiveIds));
    }
}
